clear
use "2006-2019 AB remerged with level 2 data.dta"

log using "lapop models.smcl", replace

label define loser 1 "Did Not Vote for the President's Party" 0 "Voted for the President's Party", replace
label values loser loser

** model democratic satisfaction

mixed dem_sat C.v2x_poly loser abstainer wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer
 
mixed dem_sat C.v2x_poly##C.loser abstainer##C.v2x_poly  wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer

quietly: margins , at(v2x_poly=(0.25(.05).95) loser=(0(1)1) abstainer=0 wt_grow=(3.65) ur=1 edu=9.37 quintall=2.957 female=1  age2635=1 age3645=0 age4655=0 age5665=0 age66=0 mestizo=1 indigen=0 black=0 mulatto=0 oth_race=0)
marginsplot, recast(line) recastci(rline) xtitle( "Level of Democracy (V-Dem)") ytitle("Predicted Democratic Satisfaction") title("Satisfaction with Democracy") ciopts(lpattern(dash) lcolor(gs8)) ci2opts(lpattern(dash)) scheme(s1mono) plot2opts(lpattern(line) lcolor(black)) plotopts(lpattern(line) lcolor(gs8)) legend(rows(2))
graph save "Graph" "figure 3 democratic satisfaction.gph", replace

**model if country is democratic

mixed pais_dem C.v2x_poly loser abstainer wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer

mixed pais_dem C.v2x_poly##C.loser abstainer##C.v2x_poly  wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer

quietly: margins , at(v2x_poly=(0.25(.05).95) loser=(0(1)1) abstainer=0 wt_grow=(3.65) ur=1 edu=9.37 quintall=2.957 female=1  age2635=1 age3645=0 age4655=0 age5665=0 age66=0 mestizo=1 indigen=0 black=0 mulatto=0 oth_race=0)
marginsplot, recast(line) recastci(rline) xtitle( "Level of Democracy (V-Dem)") ytitle("Predicted Level of Democracy in Country") title("Country is Democratic") ciopts(lpattern(dash) lcolor(gs8)) ci2opts(lpattern(dash)) scheme(s1mono) plot2opts(lpattern(line) lcolor(black)) plotopts(lpattern(line) lcolor(gs8)) legend(rows(2))
graph save "Graph" "figure 3 country is democratic.gph", replace

** model trust in the electoral system

mixed b11 C.v2x_poly loser abstainer wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer 
 
mixed b11 C.v2x_poly##C.loser abstainer##C.v2x_poly  wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer
quietly: margins , at(v2x_poly=(0.25(.05).95) loser=(0(1)1) abstainer=0 wt_grow=(3.65) ur=1 edu=9.37 quintall=2.957 female=1  age2635=1 age3645=0 age4655=0 age5665=0 age66=0 mestizo=1 indigen=0 black=0 mulatto=0 oth_race=0)
marginsplot, recast(line) recastci(rline) xtitle( "Level of Democracy (V-Dem)") ytitle("Predicted Trust") title("Trust in Electoral Institutions") ciopts(lpattern(dash) lcolor(gs8)) ci2opts(lpattern(dash)) scheme(s1mono) plot2opts(lpattern(line) lcolor(black)) plotopts(lpattern(line) lcolor(gs8)) legend(rows(2))
graph save "Graph" "figure 3 trust electoral institutions.gph", replace

** model trust in elections

mixed b47a C.v2x_poly loser abstainer wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer 
 
mixed b47a C.v2x_poly##C.loser abstainer##C.v2x_poly wt_grow ur edu quintall female  age2635 age3645 age4655 age5665 age66 mestizo indigen black mulatto oth_race || pais: || level2: loser abstainer
quietly: margins , at(v2x_poly=(0.25(.05).95) loser=(0(1)1) abstainer=0 wt_grow=(3.65) ur=1 edu=9.37 quintall=2.957 female=1  age2635=1 age3645=0 age4655=0 age5665=0 age66=0 mestizo=1 indigen=0 black=0 mulatto=0 oth_race=0)
marginsplot, recast(line) recastci(rline) xtitle( "Level of Democracy (V-Dem)") ytitle("Predicted Trust") title("Trust in Elections") ciopts(lpattern(dash) lcolor(gs8)) ci2opts(lpattern(dash)) scheme(s1mono) plot2opts(lpattern(line) lcolor(black)) plotopts(lpattern(line) lcolor(gs8)) legend(rows(2))
graph save "Graph" "figure 3 trust elections.gph", replace

*note that the graphs will not do the full range of the variable automatically. So I change the y-axis ranges by hand, putting in their label values from appendix 1. Doing this avoids overstating the marginal effects. 

log close